<?php

namespace App\Http\Controllers\TeacherApi\Question;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Questions;
use App\Model\UserSubject;

class QuestionListController extends Controller
{

	// 列出所有问题数据 以及答案数据
    public function getQuestions(Request $request,Questions $questions,UserSubject $userSubject){
    	$userId = $request->id;
    	if(!empty($userId)){
    		$defSub = $userSubject->where(['user_id'=>$userId,'is_default'=>1])->first();
    		if(empty($defSub)){
    			$questionList = $questions->take(20)->with('answers')->get();
    		}else{
    			$whereInfo = ['subject'=>$defSub->subject_id];
    			if($defSub->grade_id){
    				$whereInfo['grade'] = $defSub->grade_id;
    			}
    			$questionList = $questions->where($whereInfo)->take($defSub->question_num)->with('answers')->get();
    		}
    		if(!empty($questionList)){
    			$questionList[0]['question_num'] = 20;
    			$this->data['data'] = $questionList;
    			$this->data['code'] = 1;
    			$this->data['msg'] = '加载成功！';
    		}else{
    			$this->data['msg'] = '题库没有题目了！';
    		}
    	}

    	return $this->data;

    }

}
